package com.atguigu.qqzone.controllers;

import com.atguigu.qqzone.pojo.Fruit;
import com.atguigu.qqzone.service.impl.FruitServiceImpl;
import com.atguigu.uils.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: FruitServlet
 * @Description:
 * @Author: wty
 * @Date: 2022/12/6
 */
public class FruitController {

    /**
     * 接口的多态
     */
    private FruitServiceImpl fruitServiceimpl = null;
    //private FruitServiceInterface fruitServiceInterface = new FruitServiceImpl();

    private String index(String oper, String keyword, Integer pageNumber, HttpServletRequest request) {
        List<Fruit> fruitList = null;
        // 分页查询(增加参数pageNumber)
        int pageCount = 0;
        HttpSession session = request.getSession();

        if (null == pageNumber) {
            pageNumber = 1;
        }
        // 模糊查询
        if (StringUtils.isNotEmpty(oper) && "search".equals(oper)) {
            // 相当于是模糊查询
            if (StringUtils.isEmpty(keyword)) {
                keyword = "";
            }
            pageNumber = 1;
        } else {
            // 说明此处不是模糊查询,此时keyword应该从session作用域获取

            // 分页查询(增加参数pageNumber)
            Object keywordObj = session.getAttribute("keyword");
            if (null != keywordObj) {
                keyword = (String) keywordObj;
            } else {
                keyword = "";
            }
        }
        // 将keyword从session作用域获取
        session.setAttribute("keyword", keyword);

        fruitList = fruitServiceimpl.getFruitList(pageNumber, keyword);


        // 非分页,显示全部,更新当前页的值
        session.setAttribute("pageNumber", pageNumber);

        // 获取总条数
        pageCount = fruitServiceimpl.getFruitCount(keyword);
        // 获取总页数pageCount
        session.setAttribute("pageCount", pageCount);

        // 保存到session作用域
        //HttpSession session = request.getSession();
        session.setAttribute("fruitList", fruitList);

        // 此处的视图名称是index
        // thymeleaf会将这个逻辑视图名称对应到物理视图名称中去
        // 物理视图名称 = view-prefix + 逻辑视图名称 + view-suffix
        // 所以真实视图名称: /index.html
        //super.processTemplate("index", request, response);
        return "index";
    }

    private String add(String fname, Integer price, Integer fcount, String remark, HttpServletRequest request) throws IOException {
        request.setCharacterEncoding("UTF-8");
        // 判断是doGet还是doPost
        //if (StringUtils.isEmpty(request.getParameter("fname"))
        //        || StringUtils.isEmpty(request.getParameter("price"))
        //        || StringUtils.isEmpty(request.getParameter("fcount"))) {
        //    //processTemplate("add", request, response);
        //    return "add";
        //}
        if (StringUtils.isEmpty(fname)
                || null == price
                || null == fcount) {
            //processTemplate("add", request, response);
            return "add";
        }
        fruitServiceimpl.addFruit(new Fruit(0, fname, price, fcount, remark));
        //response.sendRedirect("fruit.do");
        return "redirect:fruit.do";

    }

    private String del(Integer fid) {
        if (null != fid) {
            //fruitService
            fruitServiceimpl.delFruitByFid(fid);
            //response.sendRedirect("fruit.do");
            return "redirect:fruit.do";
        }
        return "error";
    }

    private String edit(Integer fid, HttpServletRequest request) {
        if (null != fid) {
            Fruit fruit = fruitServiceimpl.getFruitByFid(fid);
            request.setAttribute("fruit", fruit);
            //processTemplate("edit", request, response);
            return "edit";
        }
        return "error";
    }

    private String update(Integer fid, String fname, Integer price, Integer fcount, String remark) {
        //  执行更新
        fruitServiceimpl.updateFruitByFid(new Fruit(fid, fname, price, fcount, remark));
        // 资源跳转
        //response.sendRedirect("fruit.do");
        return "redirect:fruit.do";
    }
}
